package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.UacDeptMapper;
import com.csun.cmny.provider.model.constant.DeptConstant;
import com.csun.cmny.provider.model.domain.UacDept;
import com.csun.cmny.provider.model.domain.UacEmp;
import com.csun.cmny.provider.model.domain.UacMenu;
import com.csun.cmny.provider.model.enums.UacMenuStatusEnum;
import com.csun.cmny.provider.model.exceptions.UacBizException;
import com.csun.cmny.provider.model.vo.DeptVo;
import com.csun.cmny.provider.model.vo.EmpVo;
import com.csun.cmny.provider.model.vo.UserVo;
import com.csun.cmny.provider.service.UacDeptService;
import com.csun.cmny.provider.service.UacEmpService;
import com.csun.cmny.provider.utils.TreeUtil;
import com.csun.cmny.util.PublicUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2018-12-13 11:14
 * @Description:
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class UacDeptServiceImpl extends BaseService<UacDept> implements UacDeptService {

    @Resource
    private UacDeptMapper uacDeptDao;

    @Resource
    private UacEmpService uacEmpService;

    @Override
    public List<DeptVo> getDeptVoAll(Long groupId) {

        List<DeptVo> deptVoList = uacDeptDao.getDeptVoAll(groupId);

        return TreeUtil.getChildDeptVos(deptVoList, 0L);
    }

    /**
     * 通过组织Id获取部门列表
     *
     * @param groupId 部门Id
     * @return 组织Id的部门列表
     */
    @Override
    @Transactional(readOnly = true, rollbackFor = Exception.class)
    public DeptVo getDeptVoList(Long groupId) {

        if (groupId == null) {
            throw new UacBizException(ErrorCodeEnum.UAC10015001, groupId);
        }

        // 查询顶层的部门信息
        UacDept uacDeptQuery = new UacDept();
        uacDeptQuery.setGroupId(groupId);
        uacDeptQuery.setLevel(DeptConstant.DEPT_LEVEL_ROOT);
        uacDeptQuery.setPid(0L);
        uacDeptQuery = mapper.selectOne(uacDeptQuery);
        if (PublicUtil.isEmpty(uacDeptQuery)) {
            throw new UacBizException(ErrorCodeEnum.UAC10016003, "");
        }
        DeptVo deptVo = getDeptVo(uacDeptQuery);

        // 查询顶层部门-用户
        List<EmpVo> empVoList = uacEmpService.getEmpByDeptId(uacDeptQuery.getId());
        deptVo.setEmpVoList(empVoList);

        // 判断是否为叶子节点
        if (uacDeptQuery.getLeaf().equals(DeptConstant.DEPT_LEAF_NO)) {
            // 如果不是叶子节点, 还有子节点

        }

        return deptVo;
    }



    /**
     * 转换UacDept为DeptVo
     *
     * @param uacDept
     * @return deptVo
     */
    DeptVo getDeptVo(UacDept uacDept) {

        DeptVo deptVo = new DeptVo();
        deptVo.setId(uacDept.getId());
        deptVo.setDeptName(uacDept.getDeptName());
        deptVo.setNumber(uacDept.getNumber());
        deptVo.setPid(uacDept.getPid());
        deptVo.setRemark(uacDept.getRemark());

        return deptVo;
    }

    /**
     * 保存部门信息
     *
     * @param uacDept      部门信息
     * @param loginAuthDto 用户登录信息
     * @return 影响条数
     */
    @Override
    public int saveUacDept(UacDept uacDept, LoginAuthDto loginAuthDto) {

        Long pid = uacDept.getPid();
        uacDept.setUpdateInfo(loginAuthDto);

        UacDept parentDept = mapper.selectByPrimaryKey(pid);

        if (PublicUtil.isEmpty(parentDept)) {
            throw new UacBizException(ErrorCodeEnum.UAC10016001, pid);
        }

        if (uacDept.isNew()) {
            // 更新父节点的叶子信息
            UacDept updateDept = new UacDept();
            updateDept.setLeaf(DeptConstant.DEPT_LEAF_NO);
            updateDept.setId(pid);
            int result = mapper.updateByPrimaryKeySelective(updateDept);
            if (result < 1) {
                throw new UacBizException(ErrorCodeEnum.UAC10016002, pid);
            }
            // 构建子节点的信息
//            Long deptId = super.generateId();
//            uacDept.setId(deptId);
            uacDept.setLevel(parentDept.getLevel() + 1);
            uacDept.setStatus(UacMenuStatusEnum.ENABLE.getType());
            uacDept.setGroupId(loginAuthDto.getGroupId());
            uacDept.setCreator(loginAuthDto.getUserName());
            uacDept.setCreatorId(loginAuthDto.getUserId());
            uacDept.setLastOperator(loginAuthDto.getUserName());
            uacDept.setLastOperatorId(loginAuthDto.getUserId());
            // 新增的部门是叶子节点
            uacDept.setLeaf(DeptConstant.DEPT_LEAF_YES);
            return uacDeptDao.insertSelective(uacDept);
        } else {
            return uacDeptDao.updateByPrimaryKeySelective(uacDept);
        }
    }

    /**
     * 根据部门Id删除部门信息
     *
     * @param id           部门Id
     * @param loginAuthDto 用户登录信息
     * @return 影响条数
     */
    @Override
    public int deleteUacDeptById(Long id, LoginAuthDto loginAuthDto) {

        int result;

        // 获取当前部门信息
        UacDept uacDeptQuery = new UacDept();
        uacDeptQuery.setId(id);
        uacDeptQuery = mapper.selectOne(uacDeptQuery);
        if (PublicUtil.isEmpty(uacDeptQuery)) {
            throw new UacBizException(ErrorCodeEnum.UAC10016003, id);
        }

        // 判断部门是不是根节点
        if (DeptConstant.DEPT_LEVEL_ROOT.equals(uacDeptQuery.getLevel())) {
            throw new UacBizException(ErrorCodeEnum.UAC10016011, id);
        }

        // 删除部门
        result = uacDeptDao.deleteByPrimaryKey(id);
        if (result < 1) {
            log.error("删除部门失败, deptId = {}", id);
            throw new UacBizException(ErrorCodeEnum.UAC10016008, id);
        }

        // 查询父节点是否还有子节点
        int count = uacDeptDao.selectDeptChildCountByPid(uacDeptQuery.getPid());
        if (count == 0) {
            // 修改当前删除部门的父部门是否为叶子节点
            UacDept updateParentUacDept = new UacDept();
            updateParentUacDept.setId(uacDeptQuery.getPid());
            updateParentUacDept.setLeaf(DeptConstant.DEPT_LEAF_YES);

            uacDeptDao.updateByPrimaryKeySelective(updateParentUacDept);
        }

        return result;
    }

    /**
     * 启用部门
     *
     * @param deptList     部门列表
     * @param loginAuthDto 用户登录信息
     * @return 影响条数
     */
    @Override
    public int enableDeptList(List<UacDept> deptList, LoginAuthDto loginAuthDto) {
        return 0;
    }

    /**
     * 获取部门Id所有的父级部门信息
     *
     * @param id 部门Id
     * @return 根据部门id查询出来的父级部门信息
     */
    @Override
    public List<UacDept> getAllParentDeptByDeptId(Long id) {
        return null;
    }

    /**
     * 获取所有部门的子部门信息
     *
     * @param id         部门Id
     * @param deptStatus 部门状态
     * @return 根据部门Id查询所有的子部门信息
     */
    @Override
    public List<UacDept> getAllChildDeptByDeptId(Long id, String deptStatus) {
        return null;
    }

    /**
     * 禁用部门
     *
     * @param deptList     部门列表
     * @param loginAuthDto 用户登录信息
     * @return 影响条数
     */
    @Override
    public int disableDeptList(List<UacDept> deptList, LoginAuthDto loginAuthDto) {
        return 0;
    }

    /**
     * 查询部门列表
     *
     * @param uacDept 查询条件
     * @return 结果列表
     */
    @Override
    public List<UacDept> selectDeptList(UacDept uacDept) {
        return null;
    }

    /**
     * 判断部门是否有子部门
     *
     * @param deptId 部门Id
     * @return 结果
     */
    @Override
    public boolean checkDeptHasChildDept(Long deptId) {
        return false;
    }
}
